<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :addTodo="addTodo"></Header>
      <List
        :todos="todos"
        :deleteTodo="deleteTodo"
        :updateTodo="updateTodo"
      ></List>
      <Footer
        :todos="todos"
        :checkAll="checkAll"
        :clearAllCompletedTodos="clearAllCompletedTodos"
      ></Footer>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, reactive, toRefs, watch } from "vue";
import Header from "./components/Header.vue";
import List from "./components/List.vue";
import Footer from "./components/Footer.vue";

//引入接口
import { Todo } from "./types/todo";

//引入缓存方法
import { saveTodos, readTodos } from "./utils/localStorageUtils";

export default defineComponent({
  components: { Header, List, Footer },
  name: "App",
  //数据应该用数组来存储，数组中的每一个数据都是一个对象，对象中应该有三个属性（id,title,isCompleted）
  //把数组暂且定义在App.vue父级组件
  setup() {
    //定义一个数组数据
    // const state = reactive<{ todos: Todo[] }>({
    //   todos: [
    //     { id: 1, title: "奔驰", isCompleted: false },
    //     { id: 2, title: "宝马", isCompleted: true },
    //     { id: 3, title: "奥迪", isCompleted: false },
    //   ],
    // });

    const state = reactive<{ todos: Todo[] }>({
      todos: [],
    });
    //界面加载完毕后过了一会再读取数据
    onMounted(() => {
      setTimeout(() => {
        state.todos = readTodos();
        console.log(state.todos)
      }, 1000);
    });

    //添加todo数据的方法
    const addTodo = (todo: Todo) => {
      state.todos.unshift(todo);
    };

    //删除数据的方法
    const deleteTodo = (index: number) => {
      state.todos.splice(index, 1);
    };

    //修改todo的isCompleted状态
    const updateTodo = (id: number) => {
      state.todos.forEach((todo) => {
        if (todo.id === id) {
          todo.isCompleted = !todo.isCompleted;
        }
      });
    };

    //全选或全不选的方法
    const checkAll = (isCompleted: boolean) => {
      //比较数组
      state.todos.forEach((todo) => {
        todo.isCompleted = isCompleted;
      });
    };

    //清理所有选中的数据
    const clearAllCompletedTodos = () => {
      state.todos = state.todos.filter((todo) => !todo.isCompleted);
    };

    //监视：如果todos数组的数据变化了，直接存储到浏览器的缓存中
    watch(() => state.todos, saveTodos, { deep: true });
    return {
      ...toRefs(state),
      addTodo,
      deleteTodo,
      updateTodo,
      checkAll,
      clearAllCompletedTodos,
    };
  },
});
</script>

<style>
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
